package com.hao.config;

import com.hao.mapper.UserMapper;
import com.hao.pojo.User;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
//自定义Realm
@Configuration
public class UserRealm extends AuthorizingRealm {
    @Autowired
    UserMapper userMapper;

    @Override //授权
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
       /* SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //获取当前角色
        Subject subject = SecurityUtils.getSubject();
        //获得user对象
        User principal = (User) subject.getPrincipal();
        System.out.println(principal.getState());
        //设置当前用户权限
        info.addStringPermission(principal.getState());
        return info;*/
        return null;
    }

    @Override //认证
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {



        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
            //账号验证
        User user = userMapper.selectbyname(token.getUsername());
        if (user==null){
            return null;
        }
        return new SimpleAuthenticationInfo(user,user.getPassword(),"");
    }
}
